
BuildObserver = Class.create();
BuildObserver.prototype = {
  initialize: function() {},
  onEnd: function(monitors) {},
  onError: function(build) {},
  onFailure: function(build) {},
  onSkip: function(build) {},
  onStart: function(monitors) {},
  onSuccess: function(build) {},
  notify: function(eventName, arg) {
    if(typeof(this[eventName]) == 'function') {
      this[eventName](arg);
    }else {
      alert("Can't find event function by name '" + eventName + "' on object '" + this + "'")
    }
  }
}

SpeakerObserver = Class.create();
SpeakerObserver.prototype = Object.extend(new BuildObserver(), {
  onSuccess: function(build) {
    this.say('Stand clear! ' + build.message() + build.committers());
  },

  onFailure: function(build) {
    this.say(build.message() + build.committers());
  },
  
  onError: function(build) {
    this.say(build.message());
  },

  say: function(str) {
    if(window.widget) {
      command = "/usr/bin/osascript -e 'say \"" + str + "\"'"
      window.widget.system(command, null);
    }else {
      alert(str);
    }
  }
});

BgImgView = Class.create();
BgImgView.prototype = Object.extend(new BuildObserver(), {
  initialize:function() {
    this.img = $('bg-img');
  },

  onStart: function(monitors) {
    this.working = true;
    if(monitors.length == 0) {
      this.onError(null);
    }
  },

  onSkip: function(build) {
    build.preNotifier.notify(this);
  },

  onError: function(build) {
    this.working = true;
    this.changeImg('Default.png')
    this.working = false;
  },

  onFailure: function(build) {
    this.changeImg('images/red.png')
    this.working = false;
  },

  onSuccess: function(build) {
    this.changeImg('images/green.png')
  },

  changeImg: function(src) {
    if(this.working) {
      this.img.src = src
    }
  }
});

StatusBarTextView = Class.create();
StatusBarTextView.prototype = Object.extend(new BuildObserver(), {
  initialize:function() {
    this.statusBar = $('status-bar');
    $('spinner-img').show();
  },

  onStart: function(monitors) {
    if(monitors.length == 0) {
      this.statusBar.innerHTML = 'Hey, there is nothing!';
    }else {
      this.statusBar.innerHTML = 'I am observing...';
    }
  },
  
  onFailure: function(build) {
    this.addBuildMessage(build, 'background-color:red');
  },
  
  onSuccess: function(build) {
    this.addBuildMessage(build, 'background-color:green');
  },
  
  onSkip: function(build) {
    build.preNotifier.notify(this)
  },
  
  onError: function(build) {
    this.addMessage(build, build.link(), '')
  },

  addBuildMessage: function(build, style) {
    this.addMessage(build, build.committers(), style);
  },
  
  addMessage: function(build, linkTitle, style) {
    var link = this.createLink({href: build.link(), title: linkTitle, 'style': style}, build.message());
    this.appendChild(link);
  },
  
  appendChild: function(node) {
    if(this.statusBar.innerHTML == 'I am observing...') {
      this.statusBar.innerHTML = '';
    }else {
      this.statusBar.appendChild(Builder.node('br'));
    }
    
    this.statusBar.appendChild(node);
  },
  
  createLink: function(properties, content) {
    var properties = new Hash({style: 'color: yellow', target: '_blank', onclick: 'openLink(this)'}).merge(new Hash(properties));
    return Builder.node('a', properties, content);
  }
});

StatusBarIconView = Class.create();
StatusBarIconView.prototype = Object.extend(new BuildObserver(), {
  FAILURE_ICON: 'images/orange-spinner.gif',
  SUCCESS_ICON: 'images/icon-spinner.gif',
  ERROR_ICON: 'images/white-spinner.gif',
  OBSERVING_ICON: 'images/observing.gif',
  
  initialize:function() {
    this.statusBar = $('status-bar');
    $('spinner-img').hide();
  },
  
  onStart: function(monitors) {
    this.statusBar.innerHTML = '';
    
    monitors.each(function(monitor) {
      var properties = {id: 'link_' + monitor.htmlId(), href: monitor.source, title: 'observing...', target: '_blank', onclick: 'openLink(this)'};
      var img = Builder.node('img', {id: 'img_' + monitor.htmlId(), src: this.OBSERVING_ICON, 'class': 'spinner'});
      var link =  Builder.node('a', properties, img);
      this.appendChild(link);
    }.bind(this));
  },
  
  onFailure: function(build) {
    this.addBuildMessage(build, this.FAILURE_ICON);
  },
  
  onSuccess: function(build) {
    this.addBuildMessage(build, this.SUCCESS_ICON);
  },
  
  onError: function(build) {
    this.addBuildMessage(build, this.ERROR_ICON);
  },
  
  onSkip: function(build) {
    build.preNotifier.notify(this)
  },

  addBuildMessage: function(build, icon) {
    var img = $('img_' + build.htmlId())
    var link = $('link_' + build.htmlId())

    img.src = icon
    link.title = build.name + ": " + build.message() + build.committers() + 'link: ' + build.link();
    link.href = build.link();
  },
  
  appendChild: function(node) {
    this.statusBar.appendChild(node);
  }
});
